package zhihui.mobile.presenter.myself;

import android.content.Context;
import android.graphics.Bitmap;

import com.zgm.utils.FileUtils;
import com.zgm.utils.SharedPreferencesUtis;

import org.json.JSONException;
import org.json.JSONObject;

import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.presenter.shop.BaseShopPresenter;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.myself.IUserSettingContract;

/**
 * Created by 张公铭 on 2017/9/24 0024.
 */

public class UserSettingPresenter extends BaseShopPresenter implements IUserSettingContract.IUserSettingPresenter
{
    private Context mContext;
    private IUserSettingContract.IUSerSettingView settingView;

    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        settingView = (IUserSettingContract.IUSerSettingView) view;
    }

    @Override
    public void onResume()
    {
        settingView.initViewData(UserManager.getInstance().getCurUser());
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void uploadPhoto(Bitmap bitmap)
    {
        settingView.showLoading();
        String filepath = FileUtils.createdPhotoFile(bitmap, "/picture/");
        UserManager.getInstance().uploadPhoto(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                settingView.hindLoading();
                settingView.showMessage("上传头像成功");
                try
                {
                    UserManager.getInstance().getCurUser().setPic(result.getString("data"));
                    SharedPreferencesUtis.saveSharedPreference(mContext, Config.USER_DATA_SAVE, Config.USER_DATA_SAVE, UserManager.getInstance().getCurUser());
                    settingView.updatePhoto(UserManager.getInstance().getCurUser().getPic());
                } catch (JSONException e)
                {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                settingView.hindLoading();
                settingView.showMessage(message);
            }
        },filepath);
    }
}
